package cn.jiyun.ai.common.config;

import cn.jiyun.ai.pojo.Users;
import org.apache.catalina.User;
import org.springframework.stereotype.Component;

import java.util.Random;

@Component
public class DuanConfig {

    public static com.aliyun.dysmsapi20170525.Client createClient() throws Exception {
        // 工程代码泄露可能会导致 AccessKey 泄露，并威胁账号下所有资源的安全性。以下代码示例仅供参考。
        // 建议使用更安全的 STS 方式，更多鉴权访问方式请参见：https://help.aliyun.com/document_detail/378657.html。
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                // 必填，请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。
                .setAccessKeyId("LTAI5tLPUuJvDwdpwmmq7vk7")
                // 必填，请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
                .setAccessKeySecret("6vNDgbgDsygUzpO5nlB3O2tDatgiak");
        // Endpoint 请参考 https://api.aliyun.com/product/Dysmsapi
        config.endpoint = "dysmsapi.aliyuncs.com";
        return new com.aliyun.dysmsapi20170525.Client(config);
    }

    public Integer duanConfig(Users users) throws Exception {
        Random random = new Random();
        Integer yzm=random.nextInt(9000)+1000;
        com.aliyun.dysmsapi20170525.Client client = DuanConfig.createClient();
        com.aliyun.dysmsapi20170525.models.SendSmsRequest sendSmsRequest = new com.aliyun.dysmsapi20170525.models.SendSmsRequest()
                .setPhoneNumbers(users.getPhone())
                .setSignName("左的短信")
                .setTemplateParam("{\"code\":"+yzm+"}")
                .setTemplateCode("SMS_475260631");
        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
        client.sendSmsWithOptions(sendSmsRequest, runtime);
        return yzm;
    }
}
